using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;

namespace SecurityLinkUp.Admin
{
    public partial class Presentations : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // before we do anything, ensure that the user has the right
            // role to perform anything on this page
            Security.CheckAdminUser(this);

            DrawPresentationList();
            DrawAcceptedPresentationsList();
        }

        protected void DrawPresentationList()
        {
            ArrayList presentations = MeetingData.GetMeetingPresentations(new Guid(Page.Request.QueryString["MeetingID"]));
            
            Table tbl = new Table();
            TableHeaderRow thr = new TableHeaderRow();
            TableHeaderCell speakerheader = new TableHeaderCell();
            speakerheader.Text = "Speaker";
            thr.Cells.Add(speakerheader);
            TableHeaderCell titleheader = new TableHeaderCell();
            titleheader.Text = "Title";
            thr.Cells.Add(titleheader);
            TableHeaderCell durationheader = new TableHeaderCell();
            durationheader.Text = "Duration";
            thr.Cells.Add(durationheader);
            TableHeaderCell acceptedheader = new TableHeaderCell();
            acceptedheader.Text = "Accepted";
            thr.Cells.Add(acceptedheader);
            tbl.Rows.Add(thr);

            foreach (Presentation p in presentations)
            {
  
                User usr = UserData.GetUser(p.userid);

                TableRow tr = new TableRow();
                TableCell speakercell = new TableCell();
                speakercell.Text = usr.name;
                tr.Cells.Add(speakercell);
                TableCell titlecell = new TableCell();
                titlecell.Text = p.title;
                tr.Cells.Add(titlecell);
                TableCell durationcell = new TableCell();
                durationcell.Text = p.estimatedduration;
                tr.Cells.Add(durationcell);
                TableCell acceptedcell = new TableCell();
                CheckBox acceptedchk = new CheckBox();
                acceptedchk.ID = p.uid.ToString();
                if (p.accepted)
                {
                    acceptedchk.Checked = true;
                }
                acceptedchk.CheckedChanged += new EventHandler(PresentationChk_CheckedChanged);
                acceptedchk.AutoPostBack = true;
                acceptedcell.Controls.Add(acceptedchk);
                tr.Cells.Add(acceptedcell);
                tbl.Rows.Add(tr);
                
            }

            PresentatonsPlaceholder.Controls.Add(tbl);
        }

        protected void DrawAcceptedPresentationsList()
        {
            ArrayList presentations = MeetingData.GetMeetingPresentations(new Guid(Page.Request.QueryString["MeetingID"]));
            ArrayList accepted = new ArrayList();
            foreach (Presentation p in presentations)
            {
                if (p.accepted)
                {
                    accepted.Add(p);
                }
            }
            AcceptedPresentations.DataSource = accepted;
            AcceptedPresentations.AllowReorder = true;
            AcceptedPresentations.DataBind();
            
        }

        protected void PresentationChk_CheckedChanged(object sender, EventArgs e)
        {
            // get the presentations for the item that has just been changed
            CheckBox chk = (CheckBox)sender;
            string id = chk.ID;
            Presentation p = MeetingData.GetPresentation(new Guid(id));
            
            // update the checked field and save back to db
            p.accepted = chk.Checked;
            MeetingData.SavePresentationToDB(p);

            // refresh the accepted reorder list
            DrawAcceptedPresentationsList();
        }

        protected void SaveOrderBtn_Click(object sender, EventArgs e)
        {

        }

        protected void AcceptedPresentations_ItemReorder(object sender, ReorderListItemReorderEventArgs e)
        {
            // BUGBUG: Having the presentation ID in a hidden field isnt the most secure way of
            // doing this, but the worst that someone could do in modifying this is move the order
            // of an existing meeting, or cause an error (if the meeting id doesnt exist)
            
            // save the list with the new order
            for (int i = 0; i< AcceptedPresentations.Items.Count; i++)
            {
                HiddenField h = (HiddenField)AcceptedPresentations.Items[i].FindControl("PresentationID");
                Presentation p = MeetingData.GetPresentation(new Guid(h.Value));
                p.slotnumber = i;
                MeetingData.SavePresentationToDB(p);
            }
            
        }

        
    }
}
